package com.panshi.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.panshi.po.AnswerAudit;
import com.panshi.vo.*;
import com.panshi.modules.domain.*;
import com.panshi.modules.mapper.*;
import com.panshi.modules.service.QaAnswerUncheckService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 问答审核业务层
 */
@Service
public class QaAnswerUncheckServiceImpl extends ServiceImpl<QaAnswerUncheckMapper, QaAnswerUncheck> implements QaAnswerUncheckService {

    @Resource
    QaAnswerUncheckMapper qaAnswerUncheckMapper;
    @Resource
    QaAnswerMapper qaAnswerMapper;
    @Resource
    UserMemberMapper userMemberMapper;
    @Resource
    UserDynamicMapper userDynamicMapper;
    @Resource
    QaQuestionMapper qaQuestionMapper;
    @Resource
    UserNoticeMapper userNoticeMapper;

    @Autowired
    UserDynamic userDynamic;
    @Autowired
    UserNotice userNotice;


    @Override
    public QaAnswerUncheck insert(SaveAnswerVO saveAnswerVO) {
        if(saveAnswerVO.getQuestionId()==0
                || saveAnswerVO.getUserId()==0){//当用户id和问题id不存在时直接返回
            return null;
        }

        //判断一下用户是否回答过了
        QueryWrapper<QaAnswerUncheck> wrapper = new QueryWrapper<>();
        wrapper
                .eq("question_id",saveAnswerVO.getQuestionId())
                .eq("user_id",saveAnswerVO.getUserId());
        QaAnswerUncheck qaAnswerUncheck = qaAnswerUncheckMapper.selectOne(wrapper);

        if(StringUtils.isEmpty(qaAnswerUncheck)){ //没回答进入
            qaAnswerUncheck = getQaAnswerUncheck(saveAnswerVO);
            qaAnswerUncheckMapper.insert(qaAnswerUncheck);
        }else{//用户回答过了
            return null;
        }

        return qaAnswerUncheck;
    }


    @Override
    public List<AnswerAudit> queryAnswer(QueryAnswerAuditVO queryAnswerAuditVO) {
        List<QaAnswerUncheck> unchecks = qaAnswerUncheckMapper.selectPage(queryAnswerAuditVO);
        List<AnswerAudit> answerAudit = getQaAnswerUncheckVO(unchecks);
        return answerAudit;
    }

    @Override
    public int del(Long id) {
        return qaAnswerUncheckMapper.deleteById(id);
    }

    @Override
    public boolean updStatus(UpdAnswerUncheckedStatusVO updAnswerUncheckedStatusVO) {

        //先根据id进行查询数据
        QaAnswerUncheck qaAnswerUncheck = qaAnswerUncheckMapper.selectById(updAnswerUncheckedStatusVO.getId());

        //查询一下当前用户是否回答过，如果回答过，则对数据进行修改
        QueryWrapper<QaAnswer> wrapper = new QueryWrapper<>();
        wrapper
                .eq("question_id",qaAnswerUncheck.getQuestionId())
                .eq("user_id",qaAnswerUncheck.getUserId());
        QaAnswer qaAnswer = qaAnswerMapper.selectOne(wrapper);

        if(updAnswerUncheckedStatusVO.getStatus()==1){//审核通过时
            if (!StringUtils.isEmpty(qaAnswer)){ //用户回答过
                //把回答内容进行修改
                qaAnswer.setContent(qaAnswerUncheck.getContent());
                qaAnswerMapper.updateById(qaAnswer);

                //站内消息简介
                userNotice.setCheckDescribe("修改回答审核通过");
            }else{
                //回答表新增数据
                qaAnswer.setUserId(Long.valueOf(qaAnswerUncheck.getUserId()));
                qaAnswer.setQuestionId(Long.valueOf(qaAnswerUncheck.getQuestionId()));
                qaAnswer.setContent(qaAnswerUncheck.getContent());
                qaAnswer.setPhotoPath(qaAnswerUncheck.getPhotoPath());
                qaAnswerMapper.insert(qaAnswer);

                //修改用户的回答数量
                UpdateWrapper<UserMember> userUpdateWrapper = new UpdateWrapper<>();
                userUpdateWrapper
                        .eq("id", qaAnswerUncheck.getUserId())
                        .setSql(" answer_num = answer_num+1");
                userMemberMapper.update(null,userUpdateWrapper);

                //新增动态信息
                userDynamic.setUserId(Long.valueOf(qaAnswerUncheck.getUserId()));
                userDynamic.setType(30);
                userDynamic.setTId(qaAnswer.getId());
                userDynamicMapper.insert(userDynamic);

                //修改问题的回答数
                UpdateWrapper<QaQuestion>  qaQuestionUpdateWrapper= new UpdateWrapper<>();
                qaQuestionUpdateWrapper
                        .eq("id", qaAnswerUncheck.getQuestionId())
                        .setSql(" answer_num = answer_num+1");
                qaQuestionMapper.update(null,qaQuestionUpdateWrapper);

                //站内消息简介
                userNotice.setCheckDescribe("回答审核通过");
            }
        }
        //站内消息需要标题
        QueryWrapper<QaQuestion> queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",qaAnswerUncheck.getQuestionId()).select("question");
        QaQuestion qaQuestion = qaQuestionMapper.selectOne(queryWrapper);

        //新增站内消息
        userNotice.setTitle(qaQuestion.getQuestion());
        userNotice.setCheckDescribe(qaAnswerUncheck.getCheckDescribe());
        userNotice.setType(30);
        userNotice.setUserId(Long.valueOf(qaAnswerUncheck.getUserId()));
        userNoticeMapper.insert(userNotice);

        qaAnswerUncheck.setStatus(updAnswerUncheckedStatusVO.getStatus());
        int i = qaAnswerUncheckMapper.updateById(qaAnswerUncheck);
        boolean bool = i!=0;
        return bool;
    }

    @Override
    public Integer queryAuditCount(QueryAnswerAuditVO queryAnswerAuditVO) {
        QueryWrapper<QaAnswerUncheck> wrapper = new QueryWrapper<>();
        if (queryAnswerAuditVO.getUserId()!=0L){//用户
            wrapper.eq("user_id",queryAnswerAuditVO.getUserId());
        }
        if (queryAnswerAuditVO.getStatus()!=0){
            wrapper.eq("status",queryAnswerAuditVO.getStatus()-1);
        }
        Integer count = qaAnswerUncheckMapper.selectCount(wrapper);
        return count;
    }

    @Override
    public boolean updateById(UpdAnswerUncheckedVO updAnswerUncheckedVO){
        //当回答内容发生变动时，需要重新审核
        QaAnswerUncheck qaAnswerUncheckVO = new QaAnswerUncheck();

        qaAnswerUncheckVO.setStatus(0);
        qaAnswerUncheckVO.setId(updAnswerUncheckedVO.getId());
        qaAnswerUncheckVO.setContent(updAnswerUncheckedVO.getContent());

        int i = qaAnswerUncheckMapper.updateById(qaAnswerUncheckVO);
        boolean bool = i!=0;
        return bool;
    }

    /*SaveAnswerVO 转 QaAnswerUncheck*/
    public QaAnswerUncheck getQaAnswerUncheck(SaveAnswerVO saveAnswerVO){
        QaAnswerUncheck qaAnswerUncheck = new QaAnswerUncheck();
        if(saveAnswerVO == null){
            return null;
        }
        BeanUtils.copyProperties(saveAnswerVO,qaAnswerUncheck);
        return qaAnswerUncheck;
    }


    // QaAnswerUncheck # AnswerAudit
    public List<AnswerAudit> getQaAnswerUncheckVO(List<QaAnswerUncheck> qaAnswerUnchecks){
        List<AnswerAudit> list = new ArrayList<>();
        if(qaAnswerUnchecks == null){
            return null;
        }
        for (QaAnswerUncheck item:qaAnswerUnchecks) {
            AnswerAudit qaAnswerUncheckVO = new AnswerAudit();
            BeanUtils.copyProperties(item,qaAnswerUncheckVO);
            qaAnswerUncheckVO.setNickname(item.getUserMember().getNickname());
            list.add(qaAnswerUncheckVO);
        }
        return list;
    }
}
